﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;
using System.Drawing.Printing;
using System.Management;
using System.Runtime.InteropServices;
using SpreadsheetGear;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace WpfApplication1
{
    public class SystemIOHelper
    {

        #region method of ZipFile
        public static ICollection<string> UnCompress(string file, string destination)
        {
            using (ZipFile zip = new ZipFile(file, Encoding.GetEncoding(936)))
            {
                zip.ExtractAll(destination, ExtractExistingFileAction.OverwriteSilently);
                return zip.EntryFileNames;
            }
        }

        public static void Compress(string[] files, string zipFile)
        {
            using (ZipFile zip = new ZipFile(Encoding.GetEncoding(936)))
            {
                foreach (var file in files)
                {
                    zip.AddFile(file, "");
                }
                zip.Save(zipFile);
            }
        }

        public static List<FileInfo> UnCompressR41FromCop(FileInfo copinfo)
        {
            List<FileInfo> r41files = new List<FileInfo>();
            using (ZipFile zip = new ZipFile(copinfo.FullName, Encoding.GetEncoding(936)))
            {
                foreach (var entry in zip.Entries)
                {
                    if (entry.FileName.ToUpper().EndsWith(".R41"))
                    {
                        entry.Extract(copinfo.DirectoryName, ExtractExistingFileAction.OverwriteSilently);
                        r41files.Add(new FileInfo(Path.Combine(copinfo.DirectoryName, entry.FileName)));
                    }
                }
            }
            return r41files;
        }

        #endregion

        #region stream Method
        public static void WriteSteramAsFile(string content, string file, Encoding econding)
        {
            using (StreamWriter sw = new StreamWriter(file, false, econding))
            {
                sw.Write(content);
            }
        }
        #endregion

        public static void DeleteFiles(string[] files)
        {
            foreach (var file in files)
            {
                if (File.Exists(file))
                    File.Delete(file);
            }
        }

        public static void OpenFolderAndSelectFile(string fileFullName)
        {
            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
            psi.Arguments = "/e,/select," + fileFullName;
            Process.Start(psi).Dispose();
        }

    }

}
